package cn.zspt.phonebook.controller;

import cn.zspt.phonebook.domain.PhoneBook;
import cn.zspt.phonebook.service.PhoneService;
import cn.zspt.phonebook.util.ChineseToPinYin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class PhoneController {
    @Autowired
    private PhoneService phoneService;
    @GetMapping("/phoneBook")
    public List<PhoneBook> findAllPhoneBook(){
        return phoneService.findAllPhoneBook();
    }

    @GetMapping("/phoneBook/{name}")
    public List<PhoneBook> findPhoneBookByName(@PathVariable(name = "name") String keyword){
        return phoneService.findPhoneBookByName(keyword);
    }

    @PostMapping("/phoneBook")
    public void addPhoneBook(@RequestBody PhoneBook phoneBook){
        phoneBook.setInit(ChineseToPinYin.getInit(phoneBook.getName()));
        phoneService.addPhoneBook(phoneBook);
    }
    @GetMapping("/phoneBook/group")
    public Map<String,List<PhoneBook>> findAllPhoneBooksByGroup(){
        return phoneService.findAllPhoneBooksByGroup();
    }
}
